package com.xiegang.servicedriveruser.service;

import com.xiegang.internalcommon.dto.Car;
import com.xiegang.internalcommon.dto.ResponseResult;
import com.xiegang.internalcommon.response.TerminalResponse;
import com.xiegang.internalcommon.response.TrackResponse;
import com.xiegang.servicedriveruser.mapper.CarMapper;
import com.xiegang.servicedriveruser.remote.ServiceMapClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author xieGang
 * @description
 * @date 2023/3/30 14:19
 **/
@Service
public class CarService {

    @Autowired
    private CarMapper carMapper;

    @Autowired
    private ServiceMapClient serviceMapClient;

    public ResponseResult addCar(Car car) {
        LocalDateTime now = LocalDateTime.now();
        car.setGmtCreate(now);
        car.setGmtModified(now);

        carMapper.insert(car);
        //获得此车辆对应的终端id：tid
        ResponseResult<TerminalResponse> terminalResponseResponseResult = serviceMapClient.terminalAdd(car.getVehicleNo(),car.getId().toString());
        String tid = terminalResponseResponseResult.getData().getTid();
        car.setTid(tid);

        ResponseResult<TrackResponse> trackResponseResponseResult = serviceMapClient.trackAdd(tid);
        String trid = trackResponseResponseResult.getData().getTrid();
        String trname = trackResponseResponseResult.getData().getTrname();
        car.setTrid(trid);
        car.setTrname(trname);
        //获得此车辆对应的轨迹id：trid
        carMapper.updateById(car);
        return ResponseResult.success();
    }

    public ResponseResult<Car> getCarByCarId(Long carId) {

        Car car = carMapper.selectById(carId);
        return ResponseResult.success(car);
    }
}
